var spider = require('./main');
var _ = require('lodash');
var moment = require('moment');
var PostController = require('./controller/PostController');
var NewsController = require('./controller/NewsController');
var CronJob = require('cron').CronJob;
var crypto = require('crypto');
var async = require('async');

var job = new CronJob('*/5 * * * *', function(){
  async.parallel([
    
    function(done){
      /**
       * 新浪 - 新闻 - http://rss.sina.com.cn/news/marquee/ddt.xml
       */
      var news_url = 'http://rss.sina.com.cn/news/marquee/ddt.xml';
      spider.FetchRss(news_url).then(function(data){
        async.mapSeries(data,function(i,callback){
          var md5 = crypto.createHash('md5');
          NewsController.create({
            title:i.title,
            link:i.link,
            date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
            author:i.author,
            description:i.description,
            guid:md5.update(i.guid).digest('hex'),
            cate:'sina',
            c_cate:'新闻'
          },function(err){
            if(err) return done(err);
            callback(null,[]);
          });
        },function(err,res){
          done(null,'sina-新闻');
        });
      });
    },
    function(done){
      /**
       * 新浪 - 财经 - http://rss.sina.com.cn/roll/finance/hot_roll.xml
       */
      var finance_url = 'http://rss.sina.com.cn/roll/finance/hot_roll.xml';
      spider.FetchRss(finance_url).then(function(data){
        async.mapSeries(data,function(i,callback){
          var md5 = crypto.createHash('md5');
          NewsController.create({
            title:i.title,
            link:i.link,
            date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
            author:i.author,
            description:i.description,
            guid:md5.update(i.guid).digest('hex'),
            cate:'sina',
            c_cate:'财经'
          },function(err){
            if(err) return done(err);
            callback(null,[]);
          });
        },function(err,res){
          done(null,'sina-财经');
        });
      });
    },
    function(done){
      /**
       * sohu 互联网 - http://rss.it.sohu.com/rss/hulianwang.xml
       */
      var hulianwang_url = 'http://rss.it.sohu.com/rss/hulianwang.xml';
      spider.FetchRss(hulianwang_url).then(function(data){
        async.mapSeries(data,function(i,callback){
          var md5 = crypto.createHash('md5');
          NewsController.create({
            title:i.title,
            link:i.link,
            date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
            author:i.author,
            description:i.description,
            guid:md5.update(i.guid).digest('hex'),
            cate:'sohu',
            c_cate:'互联网'
          },function(err){
            if(err) return done(err);
            callback(null,[]);
          });
        },function(err,res){
          done(null,'sohu-互联网');
        });
      });
    },
    function(done){
      /**
       * 开源项目
       * @type {String}
       */
      var feel_url = 'http://www.oschina.net/project/rss';
      spider.FetchRss(feel_url).then(function(data){
        async.mapSeries(data,function(i,callback){
          var md5 = crypto.createHash('md5');
          NewsController.create({
            title:i.title,
            link:i.link,
            date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
            author:i.author ? i.author : '开源中国',
            description:i.description,
            guid:md5.update(i.guid).digest('hex'),
            cate:'oschina',
            c_cate:'开源项目'
          },function(err){
            if(err) return done(err);
            callback(null,[]);
          });
        },function(err,res){
          done(null,'oschina-开源项目');
        });
      });
    },
    function(done){
      /**
       * 开源新闻
       * @type {String}
       */
      var feel_url = 'http://www.oschina.net/news/rss';
      spider.FetchRss(feel_url).then(function(data){
        async.mapSeries(data,function(i,callback){
          var md5 = crypto.createHash('md5');
          NewsController.create({
            title:i.title,
            link:i.link,
            date: i.date == 'Invalid Date' ? '' : moment(i.date).format('YYYY-MM-DD HH:mm:ss'),
            author:i.author ? i.author : '开源中国',
            description:i.description,
            guid:md5.update(i.guid).digest('hex'),
            cate:'oschina',
            c_cate:'开源新闻'
          },function(err){
            if(err) return done(err);
            callback(null,[]);
          });
        },function(err,res){
          done(null,'oschina-开源新闻');
        });
      });
    }
  ],function(error,result){
    console.log(result);
  });
},null,true);